package com.project.dao.impl;
import com.project.bean.EmployeeBean;
import com.project.dao.IEmployeeDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: EmployeeDaoImpl
 * @Package: com.project.bean
 * @Description:
 * @Author: Liang-Xie
 * @Date: 2022/6/30 - 11:24
 */
@SuppressWarnings("all")
@Repository
public class EmployeeDaoImpl extends BaseDao implements IEmployeeDao {
    @Override
    public List<EmployeeBean> findAll() {
        List<EmployeeBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_employee");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(this.getBean());
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return list;
    }

    @Override
    public void add(EmployeeBean em) {
        this.setConnection();
        try {
            ps = con.prepareStatement("INSERT INTO t_employee(e_name,e_phone,e_money,e_faceImg,e_status)" +
                    " VALUES(?,?,?,?,?)");
            ps.setObject(1, em.getName());
            ps.setObject(2, em.getPhone());
            ps.setObject(3, em.getMoney());
            ps.setObject(4, em.getImgPath());
            ps.setObject(5, em.getStatus());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
    }

    @Override
    public void update(Integer id, Integer money) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_employee set e_money=? where pk_emId=? ");
            ps.setObject(1, money);
            ps.setObject(2, id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }

    }

    @Override
    public EmployeeBean findById(Integer id) {
        EmployeeBean employeeBean = null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_employee where pk_emId = ? ");
            ps.setObject(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                employeeBean = this.getBean();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return employeeBean;
    }

    @Override
    public void dismission(Integer id, String status) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_employee set e_status=? where pk_emId=? ");
            ps.setObject(1, status);
            ps.setObject(2, id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
    }

    private EmployeeBean getBean() {
        EmployeeBean employee = new EmployeeBean();
        try {
            employee.setId(rs.getInt("pk_emId"));
            employee.setName(rs.getString("e_name"));
            employee.setPhone(rs.getString("e_phone"));
            employee.setMoney(rs.getInt("e_money"));
            employee.setImgPath(rs.getString("e_faceImg"));
            employee.setStatus(rs.getString("e_status"));

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return employee;
    }
}
